
import Request from '../util/request'

import base64 from '../util/base64'

// wx
import wx from 'weixin-js-sdk'

export default class Pay extends Request {

  // 获取支付信息
  getPayAmount(data) {
    return this.request({
      url: '/h5/order/get-amount',
      data
    })
  }

  // 公众号openid
  get(data) {
    return this.request({
      url: '/apis/wx/user/get',
      method: 'post',
      data
    })
  }

  // mini
  _getOpenid(data) {
    return this.request({
      url: '/h5/mini/user/get-openid',
      data
    })
  }

  // h5
  _getOpenidH5(data) {
    return this.request({
      url: '/apis/wx/user/get-openid',
      data
    })
  }

  // 这个是汇聚小程序
  payPrepareMini(params) {
    return new Promise(resolve => {
      this._getOpenid({}).then(response => {
        let { data } = response
        data = base64.parse(data)
        if (!data.openid || data.openid.length < 1) {
          wx.navigateTo({
            url: '/sub_common/user/mini-login'
          })
        } else {
          this.request({
            // url: '/pay/pay/prepare',
            url: '/pay/pay-mini-third/prepare',
            data: {
              openid: data.openid,
              ...params
            }
          }).then(response => {
            resolve(response.data)
          })
        }
      })
    })
  }

  // 这个是微信小程序
  payPrepareWxMini(params) {
    return new Promise(resolve => {
      this._getOpenid({}).then(response => {
        let { data } = response
        data = base64.parse(data)
        if (!data.openid || data.openid.length < 1) {
          wx.navigateTo({
            url: '/sub_common/user/mini-login'
          })
        } else {
          this.request({
            url: '/pay/pay-wx/prepare',
            data: {
              openid: data.openid,
              ...params
            }
          }).then(response => {
            resolve(response.data)
          })
        }
      })
    })
  }

  // 微信公众号
  payPrepareWxH5(params) {
    return new Promise(resolve => {
      this.request({
        url: '/apis/pay/pay-h5/prepare',
        data: {
          ...params
        }
      }).then(response => {
        const { data } = response
        wx.config({
          debug: false,
          appId: data.appId,
          timestamp: data.timestamp,
          nonceStr: data.nonceStr,
          signature: data.signature,
          jsApiList: ['chooseWXPay']
        })
        wx.chooseWXPay(
          {
            timestamp: data.timestamp,
            nonceStr: data.nonceStr,
            package: data.package,
            // signType: 'RSA',
            signType: 'MD5',
            // paySign: data.paySign,
            paySign: data.sign,
            success: (data) => {
              resolve()
            },
            fail: (data) => {
              reject()
            },
            cancel: () => {
              reject()
            }
          }
        )
      })
    })
  }

  // 这个是汇聚公众号支付
  payPrepareWx(params) {
    return new Promise(resolve => {
      this.request({
        url: '/apis/pay/pay-third/prepare',
        data: {
          ...params
        }
      }).then(response => {
        const data = response.data.result.rc_Result
        const signature = response.data.signature

        wx.config({
          debug: false,
          appId: data.appId,
          timestamp: data.timeStamp,
          nonceStr: data.nonceStr,
          signature: signature,
          jsApiList: ['chooseWXPay']
        })
        wx.chooseWXPay(
          {
            timestamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: 'RSA',
            paySign: data.paySign,
            success: (data) => {
              resolve()
            },
            fail: (data) => {
              reject()
            },
            cancel: () => {
              reject()
            }
          }
        )
      })
    })
  }

  // 汇聚APP
  payPrepareApp(params) {
    return new Promise(resolve => {
      this.request({
        url: '/pay/app/prepare',
        data: {
          ...params
        }
      }).then(response => {
        resolve(response.data)
      })
    })

  }

}
